package com.create404.suishoupai.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.create404.suishoupai.dto.CommentedAnnouncementDto;
import com.create404.suishoupai.dto.PageVo;
import com.create404.suishoupai.entity.AnnouncementComment;
import com.create404.suishoupai.service.IAnnouncementCommentService;
import com.create404.suishoupai.utils.Result;
import com.create404.suishoupai.vo.AnnouncementCommentListItemVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author create404
 * @since 2021-07-17
 */
@Api(tags = "公告评论接口")
@RestController
@RequestMapping("/announcement-comment")
public class AnnouncementCommentController {
    @Autowired
    private IAnnouncementCommentService iAnnouncementCommentService;

    @GetMapping("/announcementCommentList")
    public Result getAnnouncementCommentList(String announcementId) {
        List<AnnouncementCommentListItemVO> announcementCommentList = iAnnouncementCommentService.getAnnouncementCommentList(announcementId);
        return Result.ok(announcementCommentList);
    }

    @PostMapping("/addAnnouncementComment")
    public Result addAnnouncementComment(String announcementId, String employeeId, String content) {
        AnnouncementComment announcementComment = new AnnouncementComment();
        announcementComment.setAnnouncementId(announcementId);
        announcementComment.setEmployeeId(employeeId);
        announcementComment.setContent(content);
        announcementComment.setCreateTime(LocalDateTime.now());
        announcementComment.setUpdateTime(LocalDateTime.now());
        announcementComment.setTime(LocalDateTime.now());
        boolean result = iAnnouncementCommentService.save(announcementComment);
        if (result) {
            return Result.ok("评论成功");
        } else {
            return Result.error("评论失败");
        }
    }


    @PostMapping("/getCommentedAnnouncement")
    public Result getCommentedAnnouncement(@RequestBody PageVo pageVo) {
        String employeeId = pageVo.getEmployeeId();
        Integer currentPage = Integer.valueOf(pageVo.getPageNum());
        Integer pageSize = Integer.valueOf(pageVo.getPageSize());
        Page<CommentedAnnouncementDto> commentedAnnouncementDtoPage = new Page<CommentedAnnouncementDto>(currentPage, pageSize);
        IPage<CommentedAnnouncementDto> page = iAnnouncementCommentService.getCommentedAnnouncement(commentedAnnouncementDtoPage, employeeId);
        return Result.ok(page);
    }
}
